<?php

abstract class AbstractDbConnection {

    //protected $transactionCounter;

    public function beginTransaction() {
        $this->query('BEGIN');
    }

    public function commitTransaction() {
        $this->query('COMMIT');
    }

    public function rollbackTransaction() {
        $this->query('ROLLBACK');
    }

	abstract public function getLastInsertedRowId();

	abstract public function query($sql, array $arrParams = array());

	protected function prepare($sql, array $arrParams = array()) {

		$res = '';

		$length = strlen($sql);
		$paramNumber = 0;

		for ($i = 0; $i < $length; $i++) {

			$sym = $sql[$i];

			if ($sym == '?') {

				if (!array_key_exists($paramNumber, $arrParams)) {
				    //Debug::showDump('$arrParams',$arrParams);
					throw new Exception("need parameter #$paramNumber for query $sql");
				}

				$val = $arrParams[$paramNumber];

				$res .= (is_numeric($val)) ?
					$val :
					"'" . $this->escape($val) . "'";

					//"'" . strtr($val,array("'" => "'"))."'";

				$paramNumber++;

			} else {
				$res .= $sym;
			}
		}

		return $res;
	}

	abstract public function isTableExists($tableName);
	abstract protected function escape($str);
}

?>